/**
 * 
 */
package com.smarthome.common.tools;

import org.apache.commons.lang.StringUtils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author dhc
 * @date 2015年10月23日 上午9:45:49
 * @title
 */
public class PinYinUtil {
	/**
	 * @Title 拼音转换
	 * @Date 2015年7月18日
	 */
	public static String getPinYin(String hanyu){
		StringBuffer buffer = new StringBuffer("");
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        char[] ch = hanyu.trim().toCharArray();
 
        try {
            for (int i = 0; i < ch.length; i++) {
                if (Character.toString(ch[i]).matches("[\u4e00-\u9fa5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                            ch[i], format);
                    buffer.append(temp[0]);
                } else {
                    buffer.append(Character.toString(ch[i]));
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
		return buffer.toString();
	}
	
	/**
	 * @title 获取拼音，将首字母转换成大写
	 * @createTime 2015年10月23日 上午10:31:02
	 * @author dhc
	 */
	public static String getUpperFirstPinyin(String hanyu){
		if((StringUtils.isBlank(hanyu))) return "";
		StringBuffer buffer = new StringBuffer("");
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        char[] ch = hanyu.trim().toCharArray();
 
        try {
            for (int i = 0; i < ch.length; i++) {
                if (Character.toString(ch[i]).matches("[\u4e00-\u9fa5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                            ch[i], format);
                //    buffer.append(temp[0].charAt(0) -32);
                    String tempStr = temp[0];
                    char[] chararr =  tempStr.toCharArray();
                    for(int j=0;j<chararr.length;j++){
                    	if(j == 0) buffer.append((char)(chararr[j]  - 32));
                    	else{
                    		buffer.append(chararr[j]);
                    	}
                    }
                    
                    //buffer.append();
                } else {
                    buffer.append(Character.toString(ch[i]));
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
		
		return buffer.toString();
	}
	
	/**
	 * @title  获取汉语拼音首字母大写
	 * @createTime 2015年10月23日 上午10:27:21
	 * @author dhc
	 */
	public static String getUpperPinYin(String hanyu){
		if((StringUtils.isBlank(hanyu))) return "";
		StringBuffer buffer = new StringBuffer("");
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        char[] ch = hanyu.trim().toCharArray();
 
        try {
            for (int i = 0; i < ch.length; i++) {
                if (Character.toString(ch[i]).matches("[\u4e00-\u9fa5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                            ch[i], format);
                    buffer.append(temp[0].charAt(0));
                } else {
                    buffer.append(Character.toString(ch[i]));
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
		
		return buffer.toString();
	}
}
